<?php
/**
 * 邀约管理
 * User: admin
 * Date: 2017/3/25
 * Time: 8:20
 */

namespace Admin\Controller;


class InviteController extends AdminController
{

    /**
     * 邀约手续费设置
     */
    public function setfee(){
      $id = I('id');
      if(IS_POST){
          $money=I('money');
          $map['id'] = $id;
          $map['money']=$money;
          $map['up_time']=NOW_TIME;
          $res = M('invite_fee')->save($map);
          if($res){
              $this->success('设置成功！',U('setfee'));
          }else{
                $this->error('设置失败');
          }
          $this->display();
      }
      $info = M('invite_fee')->find();
      $this->Assign('info',$info);
      $this->display();
    }

    /**
     * 全部邀约
     */
    public  function  invitelist(){

        $keyword =I('keyword');
        if($keyword){
            $where['demand'] = array('like','%'.$keyword.'%');
        }
        //$list = M('invite')->where($where)->order('up_time desc')->select();
        $list = $this->lists('invite',$where,'up_time desc');
        foreach ($list as &$li){
            $li['inv_name'] = M('member')->where('uid='.$li['uid'])->getField('nickname');
            $li['ying_num'] = M('invited_to')->where('inv_id='.$li['id'])->count();
            $li['ying_ty']  =M('invited_to')->where('inv_id='.$li['id'].' and status=1')->count();
            if($li['pay_status'] == 1){
                $li['pay'] = '是';
            }else{
                $li['pay'] = '否';
            }
        }

        $this->assign('_list',$list);
        $this->meta_title = '邀约列表';
        $this->display('index');
    }

    /**
     * 邀约详情
     */
     public  function  details(){
         $id = I('id');
         $info = M('invite')->where('id='.$id)->find();
         $info['nickname']= M('member')->where('uid='.$info['uid'])->getField('nickname');
         $info['username']= M('ucenter_member')->where('id='.$info['uid'])->getField('username');
         $info['ying_num'] = M('invited_to')->where('inv_id='.$info['id'])->count();
         $info['ying_ty']  =M('invited_to')->where('inv_id='.$info['id'].' and status=1')->count();
         $this->assign('info',$info);
         $this->display('details');
     }

     /**
      * 应邀详情
      */
    public  function  ying(){
        $id= I('id');
        $list = M('invited_to')->where('inv_id='.$id)->order('up_time desc')->select();
        foreach ($list as &$li){
            $li['nickname']= M('member')->where('uid='.$li['uid'])->getField('nickname');
            $li['username']= M('ucenter_member')->where('id='.$li['uid'])->getField('username');
            if($li['status'] == 1){
                $li['statu'] = '同意';
            }else if($li['status'] == 2){
                $li['statu']  = '拒绝';
            }else{
                $li['statu']  = '未邀请';
            }
        }
        $this->assign('id',$id);
        $this->assign('_list',$list);
        $this->display('ying');
    }

    /**
     * 删除邀约
     */
    public function delete(){
        $id= I('id');
        $re = M('invite')->where('id='.$id)->delete();
        $res = M('invited_to')->where('inv_id='.$id)->delete();
        if($re){
            $this->success('删除成功',U('invitelist'));
        }else{
            $this->error('删除失败');
        }
    }

}